lib/gpg: Add helper to kill GPG agent
authorDan Nicholson <nicholson@endlessm.com>
Thu, 10 Jan 2019 19:49:17 +0000 (13:49 -0600)
committerAtomic Bot <atomic-devel@projectatomic.io>
Wed, 19 Jun 2019 17:30:24 +0000 (17:30 +0000)
With GnuPG 2, any time you do basically any operation, a gpg-agent will
be spawned for the GPG home directory in use. The classic way to kill a
gpg-agent is to use `gpg-connect-agent` and send the `killagent` command
as is done in libtest.sh.

Closes: #1799
Approved by: cgwalters

src/libotutil/ot-gpg-utils.c
src/libotutil/ot-gpg-utils.h

index cc5b0ae4e1de172efe00514335e8825452c9ca0d..cf5ce3ea7cea85ece7d650742fc18e20b00e7270 100644 (file)
@@ -437,3 +437,29 @@ ot_gpgme_new_ctx (const char *homedir,
 
   return g_steal_pointer (&context);
 }
+
+void
+ot_gpgme_kill_agent (const char *homedir)
+{
+  g_return_if_fail (homedir != NULL);
+
+  /* Run gpg-connect-agent killagent /bye */
+  g_autoptr(GError) local_error = NULL;
+  g_autoptr(GSubprocess) proc = g_subprocess_new(G_SUBPROCESS_FLAGS_STDOUT_SILENCE,
+                                                 &local_error,
+                                                 "gpg-connect-agent",
+                                                 "--homedir",
+                                                 homedir,
+                                                 "killagent",
+                                                 "/bye",
+                                                 NULL);
+  if (proc == NULL) {
+    g_debug ("Spawning gpg-connect-agent failed: %s", local_error->message);
+    return;
+  }
+  if (!g_subprocess_wait_check (proc, NULL, &local_error)) {
+    g_debug ("Killing GPG agent with gpg-connect-agent failed: %s",
+             local_error->message);
+    return;
+  }
+}
index 65ae55e4317931e010a155f755ed33e91e896864..e8a240b5977637ab79951bd1c6b88704222914cd 100644 (file)
@@ -46,4 +46,6 @@ gpgme_data_t ot_gpgme_data_output (GOutputStream *output_stream);
 gpgme_ctx_t ot_gpgme_new_ctx (const char *homedir,
                               GError    **error);
 
+void ot_gpgme_kill_agent (const char *homedir);
+
 G_END_DECLS